﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Imaginecup.shareData;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;


namespace Imaginecup.util
{

#if WINDOWS_PHONE
    public interface IMouseInputHandler
    {
        MouseState MouseState { get; }
        MouseState PreviousMouseState { get; }
    
        Vector2 Position{get;}
        bool IsOneClick();
        bool IsOneClickUp();
    }

    public class MouseInputHandler : GameComponent, IMouseInputHandler
    {
    
        private MouseState mouseState;
        private MouseState prevMouseState;

        private static MouseInputHandler m_InputHandler = null;

        private MouseInputHandler()
            : base(StateShareData.GetShareData().GetGame()) 
        {

            Game.IsMouseVisible = true;
            prevMouseState = Mouse.GetState();
            base.Initialize();
        }

         public static IMouseInputHandler GetInputHandler()
        {
            if (m_InputHandler == null)
            {
                m_InputHandler = new MouseInputHandler();
            }
            return m_InputHandler;
        }

         public bool IsOneClickUp()
         {
             if (PreviousMouseState.LeftButton == ButtonState.Pressed &&
                 MouseState.LeftButton == ButtonState.Released)
             {
                 return true;
             }
             return false;
         }
        
        public bool IsOneClick()
        {
            if( PreviousMouseState.LeftButton == ButtonState.Released &&
                MouseState.LeftButton == ButtonState.Pressed)
            {
                return true;
            }
            return false;
        }

        public sealed override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            prevMouseState = mouseState;
            mouseState = Mouse.GetState();

            base.Update(gameTime);
        }

        public Vector2 Position
        {
            get { return (new Vector2(MouseState.X, MouseState.Y));  }
        }

        public MouseState MouseState
        {
            get { return (mouseState); }
        }
        public MouseState PreviousMouseState
        {
            get { return (prevMouseState); }
        }
#endif

#if WINDOWS
    public interface IMouseInputHandler
    {
        MouseState MouseState { get; }
        MouseState PreviousMouseState { get; }

        Vector2 Position { get; }
        bool IsOneClick();
        bool IsOneClickUp();

        void SetTexture(Texture2D texture);
    }

    public class MouseInputHandler : DrawableGameComponent, IMouseInputHandler
    {
        private Texture2D m_SelectTexture;
        private Texture2D m_DefaultTexture;

        private MouseState mouseState;
        private MouseState prevMouseState;

        private ContentManager m_Content;

        private static MouseInputHandler m_InputHandler = null;

        private MouseInputHandler()
            : base(StateShareData.GetShareData().GetGame())
        {
            Game.IsMouseVisible = true;

            prevMouseState = Mouse.GetState();

            m_Content = new ContentManager(Game.Services);
            m_Content.RootDirectory = "Content";

            this.DrawOrder = 1;
            base.Initialize();
        }

        
        public static IMouseInputHandler GetInputHandler()
        {
            if (m_InputHandler == null)
            {
                m_InputHandler = new MouseInputHandler();
            }
            return m_InputHandler;
        }

        public void SetTexture(Texture2D texture)
        {
            m_SelectTexture = texture;
        }

        protected override void LoadContent()
        {
            m_DefaultTexture = m_Content.Load<Texture2D>("util\\cursor_arrow"); 
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            m_SelectTexture = null;
            m_DefaultTexture.Dispose();
            m_DefaultTexture = null;

            m_Content.Unload();
            base.UnloadContent();
        }

        public bool IsOneClickUp()
        {
            if (PreviousMouseState.LeftButton == ButtonState.Pressed &&
                MouseState.LeftButton == ButtonState.Released)
            {
                return true;
            }
            return false;
        }

        public bool IsOneClick()
        {
            if (PreviousMouseState.LeftButton == ButtonState.Released &&
                MouseState.LeftButton == ButtonState.Pressed)
            {
                return true;
            }
            return false;
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = GraphicShareData.GetShareData().GetSpriteBatch();

            if( m_SelectTexture == null )
            {
                m_SelectTexture = m_DefaultTexture;
            }

            //sb.Begin();
            //sb.Draw(m_SelectTexture, Position, Color.White);
            //sb.End();

            base.Draw(gameTime);
        }

        public sealed override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            prevMouseState = mouseState;
            mouseState = Mouse.GetState();

            base.Update(gameTime);
        }

        public Vector2 Position
        {
            get { return (new Vector2(MouseState.X, MouseState.Y)); }
        }

        public MouseState MouseState
        {
            get { return (mouseState); }
        }
        public MouseState PreviousMouseState
        {
            get { return (prevMouseState); }
        }
#endif

#if XBOX
     public interface IMouseInputHandler
    {
        GamePadState GamePadState { get; }
        GamePadState PreviousPadState { get; }
        Vector2 Position{get;}
        Vector2 PrevPosition{get;}
        bool IsOneClick();        
        bool IsOneClickUp();

    }

    public class MouseInputHandler : GameComponent, IMouseInputHandler
    {
        private GamePadState padState;
        private GamePadState prevState;

        private Vector2 m_PrevPosition;
        private Vector2 m_Position = new Vector2(300 ,300);

        private static MouseInputHandler m_InputHandler = null;

        private MouseInputHandler()
            : base(StateShareData.GetShareData().GetGame()) 
        {

            Game.IsMouseVisible = true;
            padState = GamePad.GetState(PlayerIndex.One);
            base.Initialize();
         }

         public static IMouseInputHandler GetInputHandler()
        {
            if (m_InputHandler == null)
            {
                m_InputHandler = new MouseInputHandler();
            }
            return m_InputHandler;
        }
         public bool IsOneClick()
        {
            if( prevState.Buttons.A == ButtonState.Released &&
                padState.Buttons.A == ButtonState.Pressed)
            {
                return true;
            }
            return false;
        }

         public bool IsOneClickUp()
        {
            if( prevState.Buttons.A == ButtonState.Pressed &&
                padState.Buttons.A == ButtonState.Released)
            {
                return true;
            }
            return false;
        }

        public sealed override void Update(GameTime gameTime)
        {


            m_PrevPosition = m_Position;

            if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed)
            {
                m_Position.Y += 2;
            }
            if (GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed)
            {
                m_Position.Y -= 2;
            }
            if (GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed)
            {
                m_Position.X -= 2;
            }
            if (GamePad.GetState(PlayerIndex.One).DPad.Right == ButtonState.Pressed)
            {
                m_Position.X += 2;
            }

            prevState = padState;
            padState = GamePad.GetState(PlayerIndex.One);

            base.Update(gameTime);
        }

    

        public Vector2 Position
        {
            get { return m_Position;  }
        }

        public Vector2 PrevPosition
        {
            get { return m_PrevPosition; }
        }


        public GamePadState GamePadState
        {
            get { return (padState); }
        }
        public GamePadState PreviousPadState
        {
            get { return (prevState); }
        }
        
#endif

    }

    
}